package com.qk.management.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qk.common.constant.ClueStatusConstants;
import com.qk.dto.clue.ClueListDTO;
import com.qk.entity.Clue;
import com.qk.management.mapper.ClueMapper;
import com.qk.management.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;
import com.qk.common.Result;
import com.qk.entity.Activity;
import com.qk.management.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.qk.common.PageResult;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/activities")
@SuppressWarnings("all")
public class ActivityController {

    @Autowired
    private ActivityService activityService;
    @Autowired
    private ClueService clueService;

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        //查询 线索列表如果线索中包含该活动的则不能删除
        List<Clue> clueList = clueService.list(Wrappers.lambdaQuery(Clue.class));
        for (Clue clue : clueList) {
            if (clue.getActivityId().equals(id)) {
                return Result.error("线索中包含该活动，请先删除线索");
            }
        }

        log.info("删除活动id:{}",id);

        activityService.deleteById(id);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Activity activity){
        log.info("修改活动:{}",activity);
        activityService.update(activity);
        return Result.success();
    }

    // 根据ID查询活动
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        log.info("查询活动id:{}",id);
        Activity activity = activityService.getById(id);
        return Result.success(activity);
    }

    // 活动列表查询（分页）
    @GetMapping
    public Result page(
            @RequestParam(name = "channel", required = false) Integer channel,
            @RequestParam(name = "type", required = false) Integer type,
            @RequestParam(name = "status", required = false) Integer status,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        PageResult<Activity> pageResult = activityService.page(channel, type, status, page, pageSize);
        log.info("分页查询活动,参数:渠道来源={},活动类型={},状态={},当前页码={},每页大小={}", channel, type, status, page, pageSize);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result addActivity(@RequestBody Activity activity) {
        log.info("新增活动:{}",activity);
        activityService.addActivity(activity);
        return Result.success();
    }

    // 查询指定类型的活动
    @GetMapping("/type/{type}")
    public Result listByType(@PathVariable Integer type) {
        log.info("根据类型查询活动,活动类型:{}", type);
        List<Activity> activities = activityService.listByType(type);
        return Result.success(activities);
    }
}